package org.traffometer.traffobase.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;

//import javax.servlet.GenericServlet;
//import javax.servlet.ServletContext;
//import javax.servlet.http.HttpServlet;

import org.traffometer.common.data.TraffoPacket;
import org.traffometer.traffobase.model.DerbyPacketStore;
import org.traffometer.traffobase.model.PacketStore;

//import org.traffometer.traffobase.webapp.control.Controller;

/**
 * Starts the TraffoBase server.
 * 
 * @author Robert S. Brewer
 */
@SuppressWarnings("serial")
public class Main {

  private static String dbURL = "jdbc:derby://localhost:1527/traffoPacketdb;create=true";

  // private static final String dbLocation = System.getProperty("dbPlace");

  /**
   * Empty constructor as this class is never intended to be instantiated, but this keeps PMD et al
   * happy.
   */
  private Main() {
    // empty constructor as this class is never intended to be instantiated.
  }

  /**
   * Returns the connection to the derby network database. Called in the case of Derby persistency
   * mechanism.
   * 
   * @return the connection to the database.
   */
  private static Connection getConnection() {
    Connection conn = null;
    try {
      Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
      // Get a connection
      conn = DriverManager.getConnection(dbURL);
    }
    catch (Exception except) {
      except.printStackTrace();
    }
    return conn;
  }

  /**
   * Starts the TraffoBase server.
   * 
   * @param args holds command line arguments.
   */
  public static void main(String[] args) {
    String dbKey = "something";
    // Connection con = getConnection(dbLocation);
    Connection con = getConnection();

    String serverInterface = "";
    int port = -1;

    for (int i = 0; i < args.length; i++) {
      if ("-interface".equals(args[i])) {
        serverInterface = args[i + 1];
      }
      else if ("-port".equals(args[i])) {
        try {
          port = Integer.parseInt(args[i + 1]);
        }
        catch (NumberFormatException e) {
          System.err.println("Invalid port number specified, exiting.");
          return;
        }
      }
    }

    if ("".equals(serverInterface)) {
      System.err.println("No server interface specified, will use default.");
    }
    if (port == -1) {
      System.err.println("No server port specified, will use default.");
    }

    PacketStore packetStore = new DerbyPacketStore(dbKey, con);
    System.out.println("Opened packet store from DerbyPacketStore table");
    List<TraffoPacket> currentPackets = packetStore.getPackets();
    System.out.println("There are " + currentPackets.size()
        + " traffo packet(s) currently in the database");
    for (TraffoPacket packet : currentPackets) {
      System.out.println("(Latitude: " + packet.getLatitude1() + ", " + packet.getLatitude2()
          + ")  (Longitude: " + packet.getLongitude1() + ", " + packet.getLongitude2()
          + ")  (TimeStamps: " + packet.getTimeStamp1() + ", " + packet.getTimeStamp2()
          + ")  (Vehicle Type: " + packet.getVehicleType() + ")");
    }

    // Possibly replace "true" with a property value that can be changed by the Controller
    while (true) {
      PacketListener packetListener = new PacketListener(serverInterface, port, packetStore);
      packetListener.listen();
    }
  }

}
